package com.google.projectulator;

import java.util.List;

import org.apache.maven.plugin.logging.Log;

import com.google.projectulator.exception.ProcessingException;
import com.google.projectulator.exception.ProjectulatorException;

/**
 * Processor that maintains a list of subprocessors as a chain, and executes
 * them in order when called to process.
 * 
 * @author Lance Woodson
 *
 */
public class ChainedProcessor extends BaseProjectulatorDelegate implements
		Processor {
	private List<Processor> subProcessors;
	
	public void init(Config config) throws ProjectulatorException {
		for(Processor subProcessor : subProcessors) {
			subProcessor.init(config);
		}
	}
	
	@Override
	public void cleanup(Config config) throws ProjectulatorException {
		for(Processor subProcessor : subProcessors) {
			subProcessor.cleanup(config);
		}
	}
	
	public void process(Context context) throws ProcessingException {
		for(Processor subProcessor :subProcessors) {
			subProcessor.process(context);
		}
	}
	
	@Override
	public void setLog(Log log) {
		super.setLog(log);
		for(Processor subProcessor : subProcessors) {
			subProcessor.setLog(log);
		}
	}
	
	public List<Processor> getSubProcessors() {
		return subProcessors;
	}
	
	public void setSubProcessors(List<Processor> subProcessors) {
		this.subProcessors = subProcessors;
	}
	
	@Override
	public String toString() {
		return "ChainedProcessor(" + name + ")";
	}
}
